﻿using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Quickly.Identity.Policies;
using Quickly.Identity.Services.Manage;

namespace Quickly.Identity.API.Endpoints;

public static class UsersManageEndpoints
{
    public static IEndpointRouteBuilder MapUserManageApiEndpoints<TUser>(this IEndpointRouteBuilder endpoints)
        where TUser : class
    {
        ArgumentNullException.ThrowIfNull(endpoints, nameof(endpoints));

        var routeGroup = endpoints.MapGroup("")
            .WithOpenApi()
            .WithTags("AccountManage");

        routeGroup.MapPost("/list",
            async ([FromBody] QueryUsersCommand command, [FromServices] IQueryableUserStore<TUser> queryableUserStore, CancellationToken cancellationToken) =>
            {
                return await QueryUsers.ExecuteAsync<TUser>(command, queryableUserStore, cancellationToken);
            }).WithName("QueryUsers");

        routeGroup.MapPost("/detail",
            async ([FromBody] LoadUserDetailCommand command, [FromServices] IUserStore<TUser> userStore, CancellationToken cancellationToken) =>
            {
                return await LoadUserDetail.ExecuteAsync(command, userStore, cancellationToken);
            }).WithName("GetUserDetail");

        return routeGroup;
    }


}
